import uuid
import pandas as pd
import json

# 读取Excel文件
df = pd.read_excel('4.29付费表数据补入.xlsx')

# 定义一个函数来转换行数据并添加UUID
def convert_to_log_format(row):
    orderID = row["orderid"]
    properties = {
        "#lib": row["#lib"],
        "#lib_version": row["#lib_version"],
        "#zone_offset": row["#zone_offset"],
        "MapID": row["mapid"],
        "accNum": str(row["accnum"]),
        "advType": row["advtype"],
        "areaID": row["areaid"],
        "channelPay": row["channelpay"],
        "channelType": row["channeltype"],
        "deviceID": row["deviceid"],
        "mapRate": row["maprate"],
        "orderID": "" if pd.isna(row["orderid"])  else int(row["orderid"]),
        "os_type": row["os_type"],
        "payAccount": int(row["payaccount"]*100),
        "payDevice": row["paydevice"],
        "payGift": row["paygift"],
        "payTime": str(row["paytime"]),
        "payTotal": int(row["paytotal"]*100),
        "payType": row["paytype"],
        "platform_id": row["platform_id"],
        "roleLv": row["rolelv"],
        "sdk_id": row["sdk_id"],
        "table_name": row["table_name"],
        "timeRegister": str(row["timeregister"]).replace("[\"", "").replace("\"]","")
    }
    log_format = {
        "#account_id": str(row["#account_id"]),
        "#distinct_id": str(row["deviceid"]),
        "#event_name": row["$part_event"],
        "#ip": row["#ip"],
        "#time": row["#event_time"].strftime('%Y-%m-%d %H:%M:%S.%f')[:-3],
        "#type": "track",
        "#uuid": str(uuid.uuid4()),  # 使用Python内置的uuid库生成UUID
        "properties": properties
    }
    return log_format

# 应用函数转换每一行
logs = df.apply(convert_to_log_format, axis=1)

# 打印转换后的日志字符串
with open('ta.log.2024-04-29-09_0', 'w', encoding='utf-8') as file:
    # 写入转换后的日志字符串到文件
    for log in logs:
        file.write(json.dumps(log, separators=(',', ':')) + '\n')  # 每个JSON字符串后添加换行符